﻿using UnityEngine;
using System.Collections;

//[System.Serializable]
//public class Status
//{
//    public float Health;
//    public float Damage;
//}

[System.Serializable]
public class Enemy07
{
    public bool check;

    public Transform spawnEnemy07;

    public GameObject[] subEnemy07;

}
public class EnemyController : MonoBehaviour {

    public Status status;

    public GameObject [] Explosion;

    public Transform expTransform;

    private GameObject gameExplosion;

    private PlayerController player;

    public float scaleExplosion;

    public bool Boss;

    public Enemy07 enemy07;

    public bool shoot;

    public float delayBullet;

    public float reloadTime;

    public Transform spwanBullet;

    public GameObject Bullet;

    public bool checkExplosionBullet;

    private BulletController bulletController;

    private float timeBullet;

  
    void Awake()
    {
        player = GameObject.FindGameObjectWithTag(Tags.player).GetComponent<PlayerController>();
   
    }
    void Start()
    {
       
    }

    void CreateExplosion()
    {
        gameExplosion = Instantiate(Explosion[0], expTransform.position, Quaternion.identity) as GameObject;
        gameExplosion.transform.localScale = new Vector3(scaleExplosion, scaleExplosion);
    }
   public void Hurt(float Damage)
    {
        status.Health -= Damage;
    }
   void FixedUpdate()
   {
       if (shoot)
       {
           timeBullet += Time.deltaTime;
           if (timeBullet >= delayBullet)
           {
               GameObject bullet = Instantiate(Bullet, spwanBullet.position, Bullet.transform.rotation) as GameObject;
               bullet.GetComponent<BulletController>().damage = status.Damage;
               timeBullet = reloadTime;
           }
       }
   }
    void Update()
    {
        if (status.Health <= 0 )
        {
            if (!Boss)
            {
                CreateExplosion();
            }
            else
            {
                Instantiate(Explosion[2], expTransform.position, Quaternion.identity);
            }

             if (enemy07.check)
            {
               
                 Vector3 V=enemy07.spawnEnemy07.position;
                for (int i = 0; i < 3; i++)
                {
                    Instantiate(enemy07.subEnemy07[0], new Vector3(V.x + Random.Range(-0.5f, 0.5f), V.y + Random.Range(-0.5f, 0.5f)), enemy07.subEnemy07[0].transform.rotation);
                    Instantiate(enemy07.subEnemy07[1], new Vector3(V.x + Random.Range(-0.5f, 0.5f), V.y + Random.Range(-0.5f, 0.5f)), enemy07.subEnemy07[0].transform.rotation);
                }
            }
            
          Destroy(gameObject);
        }
    }

    void OnTriggerEnter(Collider other)
    {
        if (other.tag == Tags.enemy) return;
        //if (other.tag == Tags.bulletPlayer)
        //{
        //    Vector3 tempV = other.transform.position;
        //    Debug.Log(player.status.Damage);
        //     Hurt(player.status.Damage);
        //    Destroy(other.gameObject);
        //    //if (checkExplosionBullet)
        //    //    Instantiate(Explosion[1], tempV, Quaternion.identity);
        //}

        if (other.tag == Tags.player)
        {
            if (!Boss)
            {
                CreateExplosion();
                Destroy(gameObject);

            }
        }
    }


    
}
